package com.qqtech.qquan.user.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.exception.BizzException;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.user.enums.UserFriendApplyStatusDictEnum;
import com.qqtech.qquan.user.model.UserBaseVo;
import com.qqtech.qquan.user.model.UserFriendApply;
import com.qqtech.qquan.user.model.UserFriendApplyQuery;
import com.qqtech.qquan.user.model.UserFriendApplyVo;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.qquan.user.service.UserFriendApplyService;
import com.qqtech.qquan.user.service.UserFriendService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/user/friend/apply")
public class UserFriendApplyController extends BaseWebController<UserFriendApply, UserFriendApplyQuery> {
	@Resource
	private UserFriendApplyService userFriendApplyService;
	@Resource
	private UserFriendService userFriendService;
	@Resource
	private UserBaseService userBaseService;

	@Override
	protected BaseService<UserFriendApply> getBaseService() {
		return userFriendApplyService;
	}

	/**
	 * 添加圈友申请
	 * 
	 * @param friendApply
	 *            {friendId: xxx}
	 * @return
	 * @throws BizzException
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/add")
	@ResponseBody
	public Result add(@RequestBody UserFriendApply friendApply) throws BizzException {
		Result result = new Result();
		Integer curUserId = super.getUserId();// 当前用户id
		friendApply.setUserId(curUserId);
		userFriendApplyService.addFriendApplay(friendApply);
		return result;
	}

	/**
	 * 检查用户是否我的圈友
	 * 
	 * @param friendApply
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/checkFriend")
	@ResponseBody
	public Result checkFriend(@RequestBody UserFriendApply friendApply) {
		Result result = new Result();
		Integer curUserId = super.getUserId();// 当前用户id
		Integer friendId = friendApply.getFriendId();
		if (friendId == null) {
			result.put("isMyFriend", false);
			return result;
		}
		boolean isMyFriend = userFriendService.isFriend(curUserId, friendId);
		result.put("isMyFriend", isMyFriend);
		return result;
	}

	/**
	 * 接受圈友申请
	 * 
	 * @param friendApply
	 *            {id:xxx}
	 * @return
	 * @throws BizzException
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/accept")
	@ResponseBody
	public Result accept(@RequestBody UserFriendApply friendApply) throws BizzException {
		Result result = new Result();
		Integer curUserId = super.getUserId();// 当前用户id
		Integer applyId = friendApply.getId();
		if (applyId == null) {
			result.addErr("申请id为空！");
			return result;
		}

		friendApply = userFriendApplyService.getById(applyId);
		if (friendApply == null) {
			result.addErr("找不到对应的圈友申请记录！");
			return result;
		}

		if (!curUserId.equals(friendApply.getFriendId())) {
			result.addErr("权限不足！");
			return result;
		}
		userFriendApplyService.accept(friendApply);
		return result;
	}

	/**
	 * 忽略圈友申请
	 * 
	 * @param friendApply
	 *            {id:xxx}
	 * @return
	 * @throws BizzException
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/ignore")
	@ResponseBody
	public Result ignore(@RequestBody UserFriendApply friendApply) throws BizzException {
		Result result = new Result();
		Integer curUserId = super.getUserId();// 当前用户id
		Integer applyId = friendApply.getId();
		if (applyId == null) {
			result.addErr("申请id为空！");
			return result;
		}

		friendApply = userFriendApplyService.getById(applyId);
		if (!curUserId.equals(friendApply.getFriendId())) {
			result.addErr("权限不足！");
			return result;
		}

		userFriendApplyService.ignore(friendApply);
		return result;
	}
	
	/**
	 * 删除圈友申请
	 * @param friendApply
	 *            {id:xxx}
	 * @return
	 * @throws BizzException
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/remove")
	@ResponseBody
	public Result remove(@RequestBody UserFriendApply friendApply) throws BizzException {
		Result result = new Result();
		Integer curUserId = super.getUserId();// 当前用户id
		Integer applyId = friendApply.getId();
		if (applyId == null) {
			result.addErr("申请id为空！");
			return result;
		}

		friendApply = userFriendApplyService.getById(applyId);
		if (!curUserId.equals(friendApply.getFriendId())) {
			result.addErr("权限不足！");
			return result;
		}

		userFriendApplyService.remove(friendApply);
		return result;
	}

	/**
	 * 拒绝圈友申请
	 * 
	 * @param friendApply
	 *            {id:xxx}
	 * @return
	 * @throws BizzException
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/reject")
	@ResponseBody
	public Result reject(@RequestBody UserFriendApply friendApply) throws BizzException {
		Result result = new Result();
		Integer curUserId = super.getUserId();// 当前用户id
		Integer applyId = friendApply.getId();
		if (applyId == null) {
			result.addErr("申请id为空！");
			return result;
		}

		friendApply = userFriendApplyService.getById(applyId);
		if (!curUserId.equals(friendApply.getFriendId())) {
			result.addErr("权限不足！");
			return result;
		}

		userFriendApplyService.reject(friendApply);
		return result;
	}

	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/myFriendApplyList")
	@ResponseBody
	public Result myFriendApplyList(@RequestBody UserFriendApplyQuery friendApplyQ) {
		Result result = new Result();
		UserBaseVo userVo = null;// 用于取用户呢称、头像
		Integer curUserId = super.getUserId();// 当前用户id
		friendApplyQ.setFriendId(curUserId);// 我是被邀请人
		friendApplyQ.setStatus(UserFriendApplyStatusDictEnum.WAITING.getCode());

		Pageable pageKit = new PageKit(friendApplyQ.getPage(), new PageOrder(Direction.DESC, "id"));
		List<UserFriendApplyVo> applyList = userFriendApplyService.queryList(friendApplyQ, pageKit);
		if (applyList != null && applyList.size() > 0) {
			for (UserFriendApplyVo applyVo : applyList) {
				userVo = userBaseService.getById(applyVo.getUserId());
				applyVo.setUserLogo(userVo.getLogo());
				applyVo.setUserName(userVo.getNick());
			}
		}

		result.put("friendApplyList", applyList);
		return result;
	}

	/**
	 * 面对面-扫一扫-立即加为好友
	 * 
	 * @param friendId
	 *            （对方用户id）
	 * @return
	 * @throws BizzException
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/face2face")
	@ResponseBody
	public Result face2face(@RequestBody Map<String, String> map){
		Result result = new Result();
		if (super.isVisitor()) {
			result.addErr("你还没有登陆!");
			return result;
		}
		String bizzId = map.get("bizzId").toString();
		if(bizzId==null){
			result.addErr("对方id为空!");
			return result;
		}
		Integer curUserId = super.getUserId();// 当前用户id
		Integer friendId =  Integer.parseInt(bizzId);
		UserFriendApply friendApply = new UserFriendApply();
		friendApply.setUserId(curUserId);// 申请人
		friendApply.setFriendId(friendId);

		try {
			int id = userFriendApplyService.addFriendApplay(friendApply);
			friendApply.setId(id);
			if (id <= 0) {
				result.addErr("添加圈友失败！");
				return result;
			}
			friendApply=userFriendApplyService.getById(id);
			userFriendApplyService.accept(friendApply);// 同意加为好友
		} catch (BizzException e) {
			result.addErr(e.getMsg());
		}
		return result;
	}
	
	/**
	 * 新的圈友（包括正在申请加我为好友的 和 我接受的好友申请）
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/newFriends")
	@ResponseBody
	public Result newFriends() {
		Result ret = new Result();
		UserBaseVo userVo = null;// 用于取用户呢称、头像
		Integer curUserId = super.getUserId();// 我的id
		UserFriendApplyQuery friendApply = new UserFriendApplyQuery();
		friendApply.setFriendId(curUserId);//我是被邀请的
		Pageable pageKit = new PageKit(1, new PageOrder(Direction.DESC, "id"));
		Page<UserFriendApplyVo> friendApplyPage = userFriendApplyService.queryPageList(friendApply, pageKit);
		List<UserFriendApplyVo> applyList = friendApplyPage.getContent();
		if (applyList != null && applyList.size() > 0) {
			for (UserFriendApplyVo applyVo : applyList) {
				userVo = userBaseService.getById(applyVo.getUserId());
				applyVo.setUserLogo(userVo.getLogo());
				applyVo.setUserName(userVo.getNick());
			}
		}

		ret.put("friendApplyPage", friendApplyPage);
		return ret;
	}
}
